package gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import calculation.MatrixLibrary;

import common.MyButton;
import common.MyLabel;
import common.MyTextField;

public class DialogNameResultMatrix extends JFrame {

	private FrameMain f = null;

	private MyLabel label = new MyLabel(new ImageIcon(
			"./image/dialogNameResultMatrix.png"));
	private String resultMatrixName = "Name result matrix to: ";
	private JLabel name = new JLabel(resultMatrixName);
	private MyTextField text1 = new MyTextField();

	private MyButton ok = new MyButton(new ImageIcon("./image/ok.png"));
	private MyButton cancel = new MyButton(new ImageIcon(
			"./image/cancel.png"));

	public DialogNameResultMatrix(FrameMain frameMain) {
		f = frameMain;

		setSize(451, 220);
		setResizable(false);
		label.setBounds(0, 0, 451, 220);
		name.setBounds(60, 85, 451, 25);
		text1.setBounds(205, 85, 125, 25);
		ok.setBounds(30, 120, 154, 52);
		cancel.setBounds(255, 120, 154, 52);

		label.add(name);
		label.add(text1);
		label.add(ok);
		label.add(cancel);
		this.add(label);
		this.setLocation(100, 200);

		MouseListener listenerOk = new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent arg0) {
				f.getDialogNameResultMatrix().setVisible(false);
				
				//f.getLoadingLabel().setVisible(true);

				if(f.getOutputHandler().callFortran()){
					System.out.println("Fortran success");
					DialogConfirmDone.setPath(text1.getText());
					f.getDialogConfirmDone().setVisible(true);
				}
				else{
					System.out.println("Fortran fail");
					f.getDialogConfirmError().setVisible(true);
				}
				f.getOutputHandler().copyResultFile(text1.getText());
				
				f.getInputHandler().loadMatricesFromLibraryDirectory();
				String[] nameList = (String[]) MatrixLibrary.getNameList().toArray(new String[0]);
				f.getPanelDisplayMatrixList().updateMyLayout(nameList);
				f.getPanelCalculationBar().setMatrixNameList(nameList);
				
				
				//f.getLoadingLabel().setVisible(false);
				
				
				
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}
		};
		ok.addMouseListener(listenerOk);
	}
}
